/**
 * 实体缓存管理主界面
 * @author Linzongxue
 * @create_date 2011-12-16
 */
define(function(){
	var EntityCacheMainPanel = Ext.extend(Ext.grid.GridPanel, {
		border: false,
		stripeRows: true,
	    enableHdMenu: false,
	    loadMask: true,
		initComponent: function(){
			Ext.apply(this, {
				tbar: [
					{text: '清除所有', handler: this.evictAll, iconCls: 'icon-delete', scope: this},
					'-',
					new Ext.form.TwinTriggerField({
						trigger1Class: 'x-form-clear-trigger',
		                trigger2Class: 'x-form-search-trigger',
		                hideTrigger1: true,
		                emptyText: '实体全名',
		                style:'margin-left:20px;',
		                onTrigger1Click: function(e){
		                	this.setValue('');
		                	this.ownerCt.ownerCt.findEntities(this);
		                },
		                onTrigger2Click: function(e){
		                	this.ownerCt.ownerCt.findEntities(this);
		                },
		                listeners: {specialKey: function(field, e){ //回车搜索
			                if (e.getKey() == e.ENTER) this.onTrigger2Click();
			            }}
					})
				],
				store: new Ext.data.JsonStore({
				    remoteSort: true, idProperty: 'name',
				    fields: ['name', 'countInMemory', 'countOnDisk', 'sizeInMemory'],
				    url: 'sys/util/getAllEntitiesCacheInfo.do'
				}),
				columns: [
					new Ext.grid.RowNumberer(),
					{header: '实体全名', width: 400, dataIndex: 'name'},
					{header: '内存中个数', width: 100, dataIndex: 'countInMemory'},
					{header: '磁盘中个数', width: 100, dataIndex: 'countOnDisk'},
					{header: '占内存大小', width: 100, dataIndex: 'sizeInMemory', renderer: lu.formatSize},
					{
						xtype: 'actioncolumn',width: 50,
						items: [{
		                    icon: cu.getIconImg('delete.gif'),
		                    tooltip: '清除该类所有实体的缓存',
		                    handler: function(grid, rowIndex, colIndex) {
		                        var record = grid.getStore().getAt(rowIndex);
		                        grid.evictEntityRegion(record.json.name);
		                    }
		                },{
		                	icon: cu.getIconImg('cancel.gif'),
		                    tooltip: '清除该类指定实体的缓存',
		                    handler: function(grid, rowIndex, colIndex) {
		                        var record = grid.getStore().getAt(rowIndex);
		                        grid.evictEntity(record.json.name);
		                    }
		                }]
					}
				],
				bbar:[{
					xtype:'label', text:'说明：这里的缓存是指Hibernate二级缓存中的实体对象', style:'margin-left:5px;'
				}],
				listeners: {
					afterrender: this.refresh,
					scope: this
				}
			});
			EntityCacheMainPanel.superclass.initComponent.call(this);
		},
		/**
		 * 本地查找
		 */
		findEntities: function(keyField){
			keyField.setValue(keyField.getValue().trim());
			var key = keyField.getValue().toLowerCase();
			if (key == ''){
				this.refresh();
				keyField.triggers[0].hide();
				return;
			}
			keyField.triggers[0].show();
			var grid = this;
			var num = 0;
			this.getStore().each(function(r){
				var row = this.getView().getRow(num);
				row.style.display = r.json.name.toLowerCase().indexOf(key) == -1 ? 'none' : '';
				num ++;
			}, this);
		},
		refresh: function(){
			this.getStore().load();
		},
		evictAll: function(){
			var msg = '确定要清除所有实体的缓存吗？';
			cu.confirm(msg, function(){
				cu.post({
					url:'sys/util/removeAllEntitiesFromCache.do',
					success: function(){
						cu.hint('清除成功', this.refresh, this);
					}, 
					scope: this
				});
			}, this);
		},
		evictEntity: function(entityName){
			var msg = '实体全名为：' + entityName + '<br/>' + '请输入要清除实体的主键：';
			cu.prompt(msg, function(id){
				cu.post({
					url: 'sys/util/removeEntityFromCache.do', 
					params: {entityName: entityName, id: id},
					success: function(success){
						if (success){
							cu.hint('清除成功', this.refresh, this);
						}
						else{
							cu.alert('清除失败，找不到相应的实体<br/>实体全名：' + entityName + '<br/>实体主键：' + id);
						}
					}, 
					scope: this
				});
			}, function(value){
				return Ext.num(value) > 0 ? true : '主键必须是大于零的整数';
			}, this);
		},
		evictEntityRegion: function(entityName){
			var msg = '确定要清除' + entityName + '所有实体的缓存吗？';
			cu.confirm(msg, function(){
				cu.post({
					url: 'sys/util/removeEntityRegionFromCache.do', 
					params: {entityName: entityName},
					success: function(){
						cu.hint('清除成功');
						this.refresh();
					}, 
					scope: this
				});
			}, this);
		}
	});
	return EntityCacheMainPanel;
});